package com.yeyks.appcontroller;

import com.yeyks.base.Arguments;
import com.yeyks.dto.HotelInfoDTO;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.service.HotelService;
import com.yeyks.util.UnicodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author fuchangshuai
 * @date 2019/3/26 4:27 PM
 */
@Api(tags = "酒店接口", description = "酒店")
@Slf4j
@RestController("appHotelInfoController")
@RequestMapping(value = "/app/hotel")
public class HotelInfoController {
    @Autowired
    private HotelService hotelService;

//    /**
//     * 获取如何退款
//     *
//     * @param hotelId
//     * @return
//     */
//    @ApiOperation(value = "获取退款规则")
//    @ApiImplicitParam(paramType = "query", name = "hotelId", value = "酒店id", required = true, dataType = "int")
//    @PostMapping(value = "/getRefund")
//    public ResponseEntity<?> docRefund(@RequestParam(name = "hotelId") Integer hotelId) {
//        HotelInfo hotelInfo = hotelService.selectHotelInfoById(hotelId);
//        if (hotelInfo == null || hotelInfo.getRefundCfg() == null) {
//            return ResponseEntity.ok(AjaxResponseBody.success(getRefund(null, null)));
//        }
//        JSONObject map = JSONObject.parseObject(hotelInfo.getRefundCfg());
//        Integer zero = (Integer) map.get("0");
//        Integer six = (Integer) map.get("6");
//        return ResponseEntity.ok(AjaxResponseBody.success(getRefund(zero, six)));
//    }
//
//    private List<String> getRefund(Integer zero, Integer six) {
//        List<String> list = new ArrayList<>();
//        if (zero == null || six == null) {
//            list.add("1.以入住当天12点为基准");
//            list.add("2.到达入住时间收取100%费用");
//            list.add("3.6小时内取消收取房费的5%的手续费");
//            list.add("4.提前6小时取消全额退款");
//            list.add("5.下单后30分钟未付款将取消订单");
//            return list;
//        }
//        Integer zeroPercent = zero * 100 / 1000;
//        Integer sixPercent = six * 100 / 1000;
//        list.add("1.以入住当天12点为基准");
//        list.add("2.到达入住时间收取" + zeroPercent + "%费用");
//        list.add("3.6小时内取消收取房费的" + sixPercent + "%的手续费");
//        list.add("4.提前6小时取消全额退款");
//        list.add("5.下单后30分钟未付款将取消订单");
//        return list;
//    }

    /**
     * 获取如何退款
     *
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "获取退款规则")
    @ApiImplicitParam(paramType = "query", name = "hotelId", value = "酒店id", required = true, dataType = "int")
    @PostMapping(value = "/getRefund")
    public ResponseEntity<?> docRefund(@RequestParam(name = "hotelId") Integer hotelId) {
        return ResponseEntity.ok(AjaxResponseBody.success(getRefund()));
    }

    private List<String> getRefund() {
        List<String> list = new ArrayList<>();
        list.add("1.以入住当天18:00为基准");
        list.add("2.18:00点之前全额退款");
        list.add("3.18:00点之后不能退款");
        list.add("4.下单后30分钟未付款将取消订单");
        return list;
    }

    /**
     * 用户搜索酒店
     *
     * @param city      城市名称，例：北京
     * @param checkIn   check-in日期，时间戳
     * @param checkOut  check-out日期，时间戳
     * @param price     价格（单位：分），默认值（0不限价），例：20000 （200元）
     * @param starLevel 星级：默认0查询全部，2民宿，3星级，6商务
     * @param keyword   关键字，按照酒店名称
     * @param order     推荐(排序)模式：order 0智能排序（默认是这个,按照评分降序），1距离（按照距离升序），2价格（按照价格升序），3价格（按照价格降序）
     * @param pageNum   当前页
     * @param pageSize  页码
     * @param positionX 经度
     * @param positionY 纬度
     * @return
     */
    @ApiOperation(value = "用户搜索酒店")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "checkIn", value = "入住时间", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "checkOut", value = "离开时间", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "价格(分)", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "starLevel", value = "星级(默认0查询全部，2民宿，3星级，6商务)", defaultValue = "0", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "keyword", value = "关键字", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "order", value = "0智能排序（默认是这个,按照评分降序），1距离（按照距离升序），2价格（按照价格升序），3价格（按照价格降序）", defaultValue = "0", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "当前页", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页码", defaultValue = "10", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "positionX", value = "经度", required = true, dataType = "Double"),
            @ApiImplicitParam(paramType = "query", name = "positionY", value = "纬度", required = true, dataType = "Double")
    })
    @RequestMapping(value = "/search/hotel", method = RequestMethod.POST)
    public ResponseEntity<?> searchHotel(@RequestParam(name = "city") String city,
                                         @RequestParam(name = "checkIn") Long checkIn,
                                         @RequestParam(name = "checkOut") Long checkOut,
                                         @RequestParam(name = "price") Integer price,
                                         @RequestParam(name = "starLevel", defaultValue = "0") Integer starLevel,
                                         @RequestParam(name = "keyword", required = false) String keyword,
                                         @RequestParam(name = "order", defaultValue = "0") Integer order,
                                         @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(name = "positionX") Double positionX,
                                         @RequestParam(name = "positionY") Double positionY) {
        if (Arguments.notEmpty(city)) {
            city = UnicodeUtils.unicodeToString(city);
        }
        Map<String, Object> map = hotelService.searchHotel(city, checkIn, checkOut, price, starLevel, keyword, order, pageNum, pageSize, positionX, positionY);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 用户搜索酒店新版
     *
     * @param city      城市名称，例：北京
     * @param checkIn   check-in日期，时间戳
     * @param checkOut  check-out日期，时间戳
     * @param price     价格（单位：分），默认值（0不限价），例：20000 （200元）
     * @param starLevel 星级：默认0查询全部，2民宿，3星级，6商务
     * @param positionX 经度
     * @param positionY 纬度
     * @return
     */
    @ApiOperation(value = "用户搜索酒店(星级搜索)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "checkIn", value = "入住时间(1554739200000)", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "checkOut", value = "离开时间(1554825600000)", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "价格(分)", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "starLevel", value = "星级(默认0查询全部，2民宿，3星级，6商务)", defaultValue = "0", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "positionX", value = "经度(120.117924)", required = true, dataType = "Double"),
            @ApiImplicitParam(paramType = "query", name = "positionY", value = "纬度(30.329453)", required = true, dataType = "Double")
    })
    @RequestMapping(value = "/search/hotel/new", method = RequestMethod.POST)
    public ResponseEntity<?> searchHotelNew(@RequestParam(name = "city") String city,
                                            @RequestParam(name = "checkIn") Long checkIn,
                                            @RequestParam(name = "checkOut") Long checkOut,
                                            @RequestParam(name = "price") Integer price,
                                            @RequestParam(name = "starLevel", defaultValue = "0") Integer starLevel,
                                            @RequestParam(name = "positionX") Double positionX,
                                            @RequestParam(name = "positionY") Double positionY) {
        if (Arguments.notEmpty(city)) {
            city = UnicodeUtils.unicodeToString(city);
        }
        Map<String, Object> map = hotelService.searchHotelNew(city, checkIn, checkOut, price, starLevel, positionX, positionY);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 用户搜索酒店新版
     *
     * @param city      城市名称，例：北京
     * @param checkIn   check-in日期，时间戳
     * @param checkOut  check-out日期，时间戳
     * @param price     价格（单位：分），默认值（0不限价），例：20000 （200元）
     * @param starLevel 星级：默认1舒适 2高档 3豪华 4民宿专区
     * @param positionX 经度
     * @param positionY 纬度
     * @return
     */
    @ApiOperation(value = "用户搜索酒店(舒适度搜索)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "city", value = "城市名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "checkIn", value = "入住时间(1554739200000)", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "checkOut", value = "离开时间(1554825600000)", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "价格(分)", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "starLevel", value = "星级(默认1舒适 2高档 3豪华 4民宿专区)", defaultValue = "0", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "positionX", value = "经度(120.117924)", required = true, dataType = "Double"),
            @ApiImplicitParam(paramType = "query", name = "positionY", value = "纬度(30.329453)", required = true, dataType = "Double")
    })
    @RequestMapping(value = "/search/hotel/V2", method = RequestMethod.POST)
    public ResponseEntity<?> searchHotelV2(@RequestParam(name = "city") String city,
                                            @RequestParam(name = "checkIn") Long checkIn,
                                            @RequestParam(name = "checkOut") Long checkOut,
                                            @RequestParam(name = "price") Integer price,
                                            @RequestParam(name = "starLevel", defaultValue = "0") Integer starLevel,
                                            @RequestParam(name = "positionX") Double positionX,
                                            @RequestParam(name = "positionY") Double positionY) {
        if (Arguments.notEmpty(city)) {
            city = UnicodeUtils.unicodeToString(city);
        }
        Map<String, Object> map = hotelService.searchHotelV2(city, checkIn, checkOut, price, starLevel, positionX, positionY);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 通过酒店的id查询酒店的详情
     *
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "通过酒店的id查询酒店的详情", response = HotelInfoDTO.class)
    @ApiImplicitParam(paramType = "query", name = "hotelId", value = "酒店id", required = true, dataType = "int")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseEntity<?> hotelDetail(@RequestParam(name = "hotelId") Integer hotelId) {
        HotelInfoDTO hotelInfoDTO = hotelService.selectHotelRoomDetail(hotelId);
        return ResponseEntity.ok(AjaxResponseBody.success(hotelInfoDTO));
    }
}
